package Hxw3DEngine.core.materials.shader.newFilter
{
	import Hxw3DEngine.core.Resource.geometry.ResourceGeometry;
	import Hxw3DEngine.core.Resource.texture.ResourceTexture;
	import Hxw3DEngine.core.materials.shader.filters.Filter3D;
	import Hxw3DEngine.core.materials.shader.utils.FcRegisterLabel;
	import Hxw3DEngine.core.materials.shader.utils.FsRegisterLabel;
	import Hxw3DEngine.core.materials.shader.utils.ShaderRegisterCache;
	import Hxw3DEngine.core.materials.shader.utils.ShaderRegisterElement;
	import Hxw3DEngine.core.materials.shader.utils.VcRegisterLabel;
	import Hxw3DEngine.proxy.Stage3DProxy;
	
	import flash.geom.Vector3D;
	
	public class SkyBoxFilter extends Filter3D
	{
		private var label : FsRegisterLabel;
		private var data  : Vector.<Number>;
		
		public function set texture(texture:ResourceTexture) : void {
			label.texture = texture;
		}
		
		public function SkyBoxFilter(texture:ResourceTexture)
		{
			super("SkyBoxFilter");
			data = new <Number>[0, 0, 0, 0, 1, 1, 1, 1];
			this.label = new FsRegisterLabel(texture);
		}

		override public function getVertexCode(regCache:ShaderRegisterCache):String {
			super.getVertexCode(regCache);
			var va0:ShaderRegisterElement = regCache.getVa(ResourceGeometry.POSITION);
			var vc4:ShaderRegisterElement = regCache.getVc(2,new VcRegisterLabel(data),"摄相机位置");
			var pos:Vector3D = Stage3DProxy.cameraPos;
			data[0] = pos.x;
			data[1] = pos.y;
			data[2] = pos.z;
			data[4] = data[5] = data[6] = Stage3DProxy.camera.__far/Math.sqrt(3);
			
			//var code:String = "";
			mul(regCache.op,va0,vc4.getReg(1));
			add(regCache.op,regCache.op,vc4);
			
			
			//code += "mul "+ regCache.op + ", " + va0 + ", " + vc4.getReg(1) + "\n";
			//code += "add "+ regCache.op + ", " + regCache.op + ", " + vc4 + "\n";
			
			return code;
		}
		
		override public function getFragmentCode(regCache : ShaderRegisterCache) : String {
			super.getFragmentCode(regCache);
			var fs0 : ShaderRegisterElement = regCache.getFs(label,"天空盒CUBE纹理");
			var ft0 : ShaderRegisterElement = regCache.getFt("空闲的Ft");
			
			mov(ft0, regCache.getV(ResourceGeometry.POSITION));
			tex(regCache.oc,ft0.xyz,fs0,description(label.texture));
			
			//var code : String = "";
			//{
				// 复制uv数据到ft0
			//	code += "mov " + ft0 + ", " + regCache.getV(ResourceGeometry.POSITION) + " \n";
			//	code += "tex " + regCache.oc + ", " + ft0 + ".xyz, " + fs0 + description(label.texture);
			//}
			regCache.removeFt(ft0);
			return code;
		}
		
	}
}